<?php

namespace App\Http\Controllers;

use App\InfoModel;
use Illuminate\Http\Request;

class TableController extends Controller
{
    public function index()
    {
        $list = InfoModel::get();
        return view('table',compact('list'));
    }

    public function add(Request $request)
    {
        $id = $request->input('id');
//        $uqur = InfoModel::where('id',$id)->get()[0];
        $uqur = InfoModel::where('id',$id)->first();
        return view("add",compact('uqur'));
    }

    public function addinfo(Request $request)
    {
        $data = $request->input();
//        $data = $request->except("_token");
        unset($data['_token']);
        $data['hobby'] = implode("*",$data['hobby']);
        if($data['id']==0){
            $ok = InfoModel::create($data);
        }else{
            $ok = InfoModel::where('id',$data['id'])->first();
            $ok = $ok->update($data);
        }
        if($ok){
            return "信息添加/编辑成功！<br> <a href='table'> 返回首页 </a>";
        }else{
            return "信息添加/编辑失败！";
        }
    }




    public function del(Request $request)
    {
        $id = $request->input('id');
        $uqur = InfoModel::where('id',$id)->first();
        $ok = $uqur->delete();
        if ($ok){
            return "删除成功！<br><a href='table'>返回首页</a>";
        }
    }
}
